package com.conn;

import java.io.*;

public class FileHelper {
   public static boolean genFile(String filename) {
       try {
           File f = new File(GlobalData.getStorePath());
           System.out.println("file:"+f.getName()+":"+f.getPath());
           if (!f.exists()) {
               if (f.mkdirs())
                   return false;
           }

           f = new File(GlobalData.getStorePath()+"/"+filename+".txt");
           System.out.println("path:"+f.getPath());
           f.createNewFile(); //ignore retval, no need i think
//           if (!f.createNewFile()) {
//               System.out.println("file create failed!");
//               return false;
//           } else {
//                System.out.println("create file:"+f.getName()+":"+f.getPath());
//           }
       } catch (NullPointerException ex) {
            return false;
       } catch (IOException ex) {
            return false;
       } catch (SecurityException ex) {
            return false;
       }
       return true;
   }

   public static boolean objectWrite(String filename, Object obj) {
       ObjectOutputStream oos = null;

       try {
           oos = new ObjectOutputStream(
                   new FileOutputStream(GlobalData.getStorePath()+"/"+filename+".txt"));
           oos.writeObject(obj);
           oos.close();
           return true;
       } catch (NullPointerException ex) {
            return false;
       } catch (InvalidClassException ex) {
       } catch (NotSerializableException ex) {
       } catch (IOException ex) {
       } catch (SecurityException ex) {
       }

       try {
           oos.close();
       } catch (IOException ex) {
       
       }
       return false;
   }

   public static Object objectRead(String filename) {
       Object obj;
       ObjectInputStream oos = null;

       try {
           oos = new ObjectInputStream(
                   new FileInputStream(GlobalData.getStorePath()+"/"+filename+".txt"));
           obj = oos.readObject();
           oos.close();
           return obj;
       } catch (NullPointerException ex) {
           return null;
       } catch (ClassNotFoundException ex) {
       } catch (InvalidClassException ex) {
       } catch (NotSerializableException ex) {
       } catch (IOException ex) {
       } catch (SecurityException ex) {
       }

       try {
           oos.close();
       } catch (IOException ex) {
       
       }
       return null;
   }
}
